package cn.bxc.impl;

import cn.bxc.ILoginService;
import cn.bxc.vo.WeixinTemplateMessageVO;
import cn.bxc.req.WeixinQrCodeReq;
import cn.bxc.res.WeixinQrCodeRes;
import cn.bxc.res.WeixinTokenRes;
import cn.bxc.weixin.IWeixinApiService;
import com.google.common.cache.Cache;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bxc
 * @date 2025/6/7 16:40
 **/
@Service
public class ILoginServiceImpl implements ILoginService {

    @Value("${weixin.config.app-id}")
    private String appid;
    @Value("${weixin.config.app-secret}")
    private String appSecret;
    @Value("${weixin.config.template_id}")
    private String template_id;
    @Resource
    private Cache<String,String> weixinAccessToken;
    @Resource
    private Cache<String,String> openidToken;
    @Resource
    private IWeixinApiService weixinApiService;

    @Override
    public String createQrCodeTicket() throws Exception {
        //1.获取access_token
        //先从本地缓存中获取，如果没有则调用API接口获取
        String accessToken = weixinAccessToken.getIfPresent(appid);
        if(accessToken == null){
            Call<WeixinTokenRes> call = weixinApiService.getToken("client_credential", appid, appSecret);
            WeixinTokenRes weixinTokenRes = call.execute().body();
            accessToken = weixinTokenRes.getAccess_token();
            //保存到本地缓存
            weixinAccessToken.put(appid,accessToken);
        }

        //2.生成ticket
        //包装数据发送请求去获取ticket
        WeixinQrCodeReq weixinQrCodeReq = WeixinQrCodeReq.builder()
                .expire_seconds(2592000)
                .action_name(WeixinQrCodeReq.ActionNameTypeVO.QR_SCENE.getCode())
                .action_info(WeixinQrCodeReq.ActionInfo.builder()
                        .scene(WeixinQrCodeReq.ActionInfo.Scene.builder()
                                .scene_id(100601)
                                .build())
                        .build())
                .build();
        Call<WeixinQrCodeRes> call = weixinApiService.createQrCode(accessToken, weixinQrCodeReq);
        WeixinQrCodeRes weixinQrCodeRes = call.execute().body();

        return weixinQrCodeRes.getTicket();
    }

    @Override
    public String checkLogin(String ticket) {
       return openidToken.getIfPresent(ticket);
    }

    @Override
    public void saveLoginState(String ticket, String openId) throws Exception {
        openidToken.put(ticket,openId);
        String accessToken = weixinAccessToken.getIfPresent(appid);
        if(accessToken == null){
            Call<WeixinTokenRes> call = weixinApiService.getToken("client_credential", appid, appSecret);
            WeixinTokenRes weixinTokenRes = call.execute().body();
            accessToken = weixinTokenRes.getAccess_token();
            //保存到本地缓存
            weixinAccessToken.put(appid,accessToken);
        }

        // 2. 发送模板消息
        Map<String, Map<String, String>> data = new HashMap<>();
        WeixinTemplateMessageVO.put(data, WeixinTemplateMessageVO.TemplateKey.USER, openId);

        WeixinTemplateMessageVO templateMessageDTO = new WeixinTemplateMessageVO(openId, template_id);
        templateMessageDTO.setUrl("https://gaga.plus");
        templateMessageDTO.setData(data);

        Call<Void> call = weixinApiService.sendMessage(accessToken, templateMessageDTO);
        call.execute();
    }
}
